package com.zzyl.nursing.service.impl;
/*
 *TODO
 *@Autor Adam
 *@Date  2025/7/31  21:12
 *
 */

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.CountTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询当天取消的预约的数量
     *
     * @param userId
     * @return
     */
    @Override
    public Long CancelledCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        LambdaQueryWrapper<Reservation> between = Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, userId)
                .between(Reservation::getUpdateTime, startTime, endTime);
        long count = this.list(between).stream().count();
        return count;
    }


    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<CountTimeVo> CountByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = localDateTime.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1).minusNanos(1);
        List<CountTimeVo> countTimeVoList = reservationMapper.CountByTime(startTime,endTime);
        return countTimeVoList;
    }

    /**
     * 新增预约
     * @param reservation
     */
    @Override
    public void add(Reservation reservation) {
        reservation.setStatus(0);
        this.save(reservation);
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void updateById(Long id) {
        Reservation reservation = getOne(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getId, id));
        reservation.setStatus(2);
        updateById(reservation);
    }


}
